<?php
/**
 * Nweb Editor
 *
 * This script is protected by copyright. Its use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    source
 * @package     pl.nweb.editor.model
 * @subpackage  row
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     1.0 $Id$
 * @link        https://nweb-editor.googlecode.com
 * @link        http://editor.nweb.pl
 */

namespace pl\nweb\editor\app\model\gateway\row;

use \pl\nweb\editor\app\model\gateway\table\Content as Table;

/**
 * Model wiersza tabeli
 *
 * @category    source
 * @package     pl.nweb.editor.model
 * @subpackage  row
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @version     1.0 $Id$
 */
class Content extends \pl\nweb\editor\app\model\gateway\Row
{
    /**
     * Opcje
     *
     * @var null|array
     */
    private $_options = null;

    /**
     * Wszystkie atrybuty
     *
     * @var null|array
     */
    private $_attribs = null;

    /**
     * Wybrane atrybuty
     *
     * @var null|array
     */
    private $_attrib = null;

    /**
     * Opcje
     *
     * @var null|array
     */
    private $_classObject = null;

    /**
     * Definiuje identyfikator
     *
     * @param integer $id identyfikator
     * @return void
     */
    public function setId ($id)
    {
        if($id = (int)$id) {
            $this->{Table::FIELD_ID} = $id;
        }
    }

    /**
     * Zwraca identyfikator
     *
     * @return integer
     */
    public function getId ()
    {
        return (int)$this->get(Table::FIELD_ID);
    }

    /**
     * Definiuje 2-znakowy kod języka
     *
     * @param  string $language 2-znakowy kod języka
     * @return void
     */
    public function setLanguage ($language)
    {
        $this->{Table::FIELD_LANGUAGE} = $language;
    }

    /**
     * Zwraca 2-znakowy kod języka
     *
     * @return string
     */
    public function getLanguage ()
    {
        return $this->get(Table::FIELD_LANGUAGE);
    }

    /**
     * Definiuje datę publikacji
     *
     * @param  string $date data
     * @return void
     */
    public function setPublishDate ($date)
    {
        $this->_setDate(Table::FIELD_PUBLISH_DATE, $date);
    }

    /**
     * Zwraca datę publikacji
     *
     * @param  string $format format
     * @return null|string
     */
    public function getPublishDate ($format = 'Y/m/d H:i:s')
    {
        $this->_getDate(Table::FIELD_PUBLISH_DATE, $format);
    }

    /**
     * Definiuje tytuł
     *
     * @param  string $title tytuł
     * @return void
     */
    public function setTitle ($title)
    {
        $this->{Table::FIELD_TITLE} = $title;
    }

    /**
     * Zwraca tytuł
     *
     * @return string
     */
    public function getTitle ()
    {
        return $this->get(Table::FIELD_TITLE);
    }

    /**
     * Definiuje podsumowanie
     *
     * @param  string $summary podsumowanie
     * @return void
     */
    public function setSummary ($summary)
    {
        $this->{Table::FIELD_SUMMARY} = $summary;
    }

    /**
     * Zwraca podsumowanie
     *
     * @return string
     */
    public function getSummary ()
    {
        return $this->get(Table::FIELD_SUMMARY);
    }

    /**
     * Definiuje opis
     *
     * @param  string $description opis
     * @return void
     */
    public function setDescription ($description)
    {
        $this->{Table::FIELD_DESCRIPTION} = $description;
    }

    /**
     * Zwraca opis
     *
     * @return string
     */
    public function getDescription ()
    {
        return $this->get(Table::FIELD_DESCRIPTION);
    }

    /**
     * Definiuje treść
     *
     * @param  string $content treść
     * @return void
     */
    public function setContent ($content)
    {
        $this->{Table::FIELD_CONTENT} = $content;
    }

    /**
     * Zwraca treść
     *
     * @return string
     */
    public function getContent ()
    {
        return $this->get(Table::FIELD_CONTENT);
    }

    /**
     * Definiuje klasę obiektu
     *
     * @param  string $class klasa obiektu
     * @return void
     */
    public function setClass ($class)
    {
        $this->{Table::FIELD_CLASS} = $class;
    }

    /**
     * Zwraca klasę obiektu
     *
     * @return string
     */
    public function getClass ()
    {
        return $this->get(Table::FIELD_CLASS);
    }

    /**
     * Zwraca opcje
     *
     * @return array
     */
    public function getOptions ()
    {
        if(null === $this->_options) {
            $this->_options = array();
            $json = $this->get(Table::FIELD_OPTIONS);
            if(!empty($json) && $options = json_decode($json, true)) {
                $this->_options = $options;
            }
        }
        return $this->_options;
    }

    /**
     * Zwraca wartość produktu
     *
     * @param string $name nazwa
     * @param mixed $default domyslna wartość
     * @return string|null
     */
    public function getOption ($name, $default = null)
    {
        $data = $this->getOptions();
        if(isset($data[$name])) {
            return $data[$name];
        }
        return $default;
    }

    /**
     * Definiuje dane produktu
     *
     * @param  array|object $data dane produktu
     * @return void
     */
    public function setOption ($name, $value = null)
    {
        $this->getOptions();
        if(is_array($name)) {
            foreach($name as $k => $v) {
                $this->_options[$k] = $v;
            }
        } else {
            $this->_options[$name] = $value;
        }
    }


    /**
     * Zwraca opcje
     *
     * @return array
     */
    public function getAttributes ()
    {
        if(null === $this->_attribs) {
            $this->_attribs = array();
            // fetch
        }
        return $this->_attribs;
    }

    /**
     * Zwraca obiekt atrybutu
     *
     * @param string $name nazwa
     * @param mixed $default domyślna wartość
     * @return null|\pl\nweb\editor\datatype\content\Attribute
     */
    public function getAttribute ($name, $default = null)
    {
        if(null !== $this->_attribs) {
            if(isset($this->_attribs[$name])) {
                return $this->_attrib[$name] = $this->_attribs[$name];
            }
            return $default;
        } else if (null === $this->_attrib) {

        }

        if(isset($this->_attrib[$name])) {
            return $this->_attrib[$name];
        }
        return $default;
    }

    /**
     * Definiuje obiekt atrybutu
     *
     * @param  array|object $data dane produktu
     * @return void
     */
    public function setAttribute ($name, $value)
    {
        if (null === $this->_attrib) {
            $this->_attrib = array();
        }


        if(is_array($value) && isset($value['class'])) {
            $class = $value['class'];

            if(\pl\nweb\editor\autoloader\Autoloader::loadClass($class)) {
                $params = (isset($value['params'])) ? $value['params'] : array();
                $obj = new $class ($params);
                if($obj instanceof \pl\nweb\editor\datatype\content\Attribute) {
                    $this->_attrib[$name] = $obj;
                }

            }
        } else {

        }
    }

    /**
     * Usunięcie atrybutu
     *
     * @param  array|object $data dane produktu
     * @return void
     */
    public function delAttribute ($name)
    {
        if (null !== $this->_attrib) {
            if(isset($this->_attrib[$name])) {
                unset($this->_attrib[$name]);
            }
        }

        if (null !== $this->_attribs) {
            if(isset($this->_attribs[$name])) {
                unset($this->_attribs[$name]);
            }
        }
    }

    /**
     * Definiuje flagę widoczności
     *
     * @param  boolean $flag flaga
     * @return void
     */
    public function setVisible ($flag)
    {
        $this->{Table::FIELD_VISIBLE} = ($flag) ? 1 : 0;
    }

    /**
     * Zwraca flagę widoczności
     *
     * @return boolean
     */
    public function isVisible ()
    {
        return 1 === (int)$this->{Table::FIELD_VISIBLE};
    }

    /**
     * Definiuje status
     *
     * @param  integer $status status
     * @return void
     */
    public function setStatus ($status)
    {
        $this->{Table::FIELD_STATUS} = (int)$status;
    }

    /**
     * Zwraca status
     *
     * @return integer
     */
    public function getStatus ()
    {
        return (int)$this->get(Table::FIELD_STATUS);
    }

    /**
     * Zwraca flagę czy jest to podany status
     *
     * @param  integer $status status
     * @return boolean
     */
    public function isStatus ($status)
    {
        return $status == $this->getStatus();
    }

    /**
     * Zwraca datę utworzenia obiektu
     *
     * @param  string $format format
     * @return null|string
     */
    public function getCreated ($format = 'Y/m/d H:i:s')
    {
        return $this->_getDate(Table::FIELD_CREATED, $format);
    }

    /**
     * Zwraca datę modyfikacji obiektu
     *
     * @param  string $format format
     * @return null|string
     */
    public function getModified ($format = 'Y/m/d H:i:s')
    {
        return $this->_getDate(Table::FIELD_MODIFIED, $format);
    }

    /**
     * Allows pre-update logic to be applied to row.
     * Subclasses may override this method.
     *
     * @return void
     */
    protected function _update()
    {
        $this->{Table::FIELD_MODIFIED} = new \Zend_Db_Expr('NOW()');
    }

    /**
     * Allows pre-insert logic to be applied to row.
     * Subclasses may override this method.
     *
     * @return void
     */
    protected function _insert()
    {
        $this->{Table::FIELD_CREATED} = new \Zend_Db_Expr('NOW()');
        $this->{Table::FIELD_MODIFIED} = new \Zend_Db_Expr('NOW()');
    }
}